/**
 * Created by Generator on 2017/08/03
 */
package com.nmgmd.shequ.hospital.base.restful;

import com.nmgmd.shequ.hospital.base.entity.BillEntity;
import com.nmgmd.shequ.hospital.base.service.BillService;
import com.nmghr.base.jpa.util.DynamicSpecifications;
import com.nmghr.base.jpa.util.ServletUtil;
import com.nmghr.base.jpa.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/bill", method = RequestMethod.POST)
public class BillRestful {
    @Autowired
    private BillService billService;

    @RequestMapping(value = "/findAll")
    public ResultVo<List<BillEntity>> findAll(ServletRequest request){
        Map<String , Object> searchParams = ServletUtil.getParametersStartingWithSearch(request);
        Sort sort = ServletUtil.getParametersStartingWithSort(request);
        List<BillEntity> list = billService.findAll(DynamicSpecifications.buildSpecification(searchParams, BillEntity.class), sort);
        return ResultVo.ok(list);
    }

    @RequestMapping(value = "/findAllByPage")
    public ResultVo<Page<BillEntity>> findAllByPage(@RequestParam(value = "page",defaultValue = "0") int pageNumber,
                                                       @RequestParam(value = "rows",defaultValue = "10") int pageSize,
                                                       ServletRequest request){
        Map<String,Object> searchParams = ServletUtil.getParametersStartingWithSearch(request);
        Sort sort = ServletUtil.getParametersStartingWithSort(request);
        Page<BillEntity> page = billService.findAll(
                DynamicSpecifications.buildSpecification(searchParams, BillEntity.class),
                new PageRequest(pageNumber, pageSize, sort));
        return ResultVo.ok(page);
    }

    @RequestMapping(value = "/saveData")
    public ResultVo<BillEntity> saveData(@RequestBody BillEntity entity) {
        entity.setCheckOutDate(Long.valueOf(System.currentTimeMillis()));
        BillEntity resultEntity = billService.saveAndFlush(entity);
        return ResultVo.ok(resultEntity);
    }

    @RequestMapping(value = "/deleteDataById/{id}")
    public ResultVo<String> deleteData(@PathVariable(value = "id") String id) {
        billService.delete(id);
        return ResultVo.ok("");
    }
}